<?php
namespace App\Http\Service;
use App\Repositories\Company\CompanyRepository;

class CompanyService
{

    protected $company_repository;

    public function __construct()
    {
        $this->company_repository = new CompanyRepository();
    }
    //处理获取的法人信息，zwp
    public function getLegalInfoById($uid){
        $data = $this->company_repository->getLegalById($uid);
        
        //dd($data);exit();
        if(!$data){
            return false;
        }
        return $data;
    }
    //保存法人信息，zwp
    public function saveLegalInfoById($uid,$data){
        return $this->company_repository->saveLegalInfoById($uid,$data);
    }
    //处理获取的公司信息，zwp
    public function getCompanyInfoById($uid){
        $data = $this->company_repository->getCompanyInfoById($uid);
        
        if(!$data){
            return false;
        }
        
        return $data;
    }
    //保存公司信息，zwp
    public function saveCompanyInfoById($uid,$data){
        return $this->company_repository->saveCompanyInfoById($uid,$data);
    }
    //获取公司认证资料，zwp
    public function getLicencesById($uid){
        $data = $this->company_repository->getLicencesById($uid);
        
        if(!$data){
            return false;
        }
        
        return $data;
    }
    /**zwp
     * 保存公司认证资料
     * @param array $data
     * @return bool
     */
    public function saveLicences($uid,$data){
        return $this->company_repository->saveLicences($uid,$data);
        
    }
    /**
     * zwp
     * 根据用户id获取公司id
     * @param integer $uid
     * @return integer
     */
    public function getCid($uid){
        $cid = $this->company_repository->getCid($uid);
        if(!$cid){
            return null;
        }
        return $cid;
    }
    /**
     * zwp
     * 根据公司id获取公司产品
     * @param integer $cid
     * @return array
     */
    public function getCompanyProductsByCid($cid){
        $data = $this->company_repository->getCompanyProductsByCid($cid);
        if(!$data){
            return null;
        }
        $weave = [];
        $woven = [];
        $bag = [];
        
        foreach ($data as &$val){
            /*
            if($val["del"]==1){
                continue;
            }
            */
            $val["catename"] = $this->company_repository->getCateName($val["cate"]);
            switch ($val["type"]) {
                case 1:
                    $val["type"] = "男装";

                    break;
                case 2:
                    $val["type"] = "女装";

                    break;
                case 3:
                    $val["type"] = "童装";

                    break;

            }
            switch ($val["genre"]) {
                case 1:
                    //$val["genre"] = "针织类";
                    //$value= $this->getProductsValue($val);
                    $weave[] = $val;
                    break;
                case 2:
                    //$val["genre"] = "梭织类";
                    //$value = $this->getProductsValue($val);
                    $woven[] = $val;
                    break;
                case 3:
                    //$val["genre"] = "箱包类";
                    //$value= $this->getProductsValue($val);
                    $bag[] = $val;
                    break;

            }
            
            
        }
        $arr = ["针织类" => $weave, "梭织类" => $woven, "箱包类" => $bag];
        return $arr;
    }
    /**
     * zwp
     * 过滤不必要的键值对
     * @param type $arr
     * @return type
     */
    public function getProductsValue($arr=[]){
        array_splice($arr, 7,2);
        //array_splice($arr, 3,1);
        return $arr;
    }
    /**
     * zwp
     * 保存公司产品
     * @param array $data
     * @return bool
     */
    public function saveCompanyProducts($uid,$data){
        foreach ($data as $value){
            $this->company_repository->saveCompanyProducts($value);
        }
    }
    /**zwp
     * 获取产品风格，通过公司id
     * @param integer $cid
     * @return array
     */
    public function getStyle($cid){
        $data = $this->getCompanyProductsByCid($cid);
        $data = $this->getCateData($data);
        $arrStyle = $this->getCateIdToStyle($data);
        return $arrStyle;
    }
    /**zwp
     * 合并数据
     * @param array $data
     * @return array
     */
    public function getCateData($data){
        $arr = [];
        foreach ($data as $key=>$value){
            $arr = array_merge($arr, $value);
        }
        return $arr;
    }
    /**zwp
     * 过滤数组中相同的值，
     * @param array $data
     * @return array
     */
    public function getCateIdToStyle($data){
        $arrStyle = [];
        foreach ($data as $value){
            $cate = $value["cate"];
            $style = $this->getStyleFromCate($cate);
            if(!in_array($style, $arrStyle)){
                $arrStyle[] = $style;
            }
        }
        return ["products_style"=>$arrStyle];
    }
    /**zwp
     * 通过category id，获取风格，
     * @param integer $cate
     * @return string
     */
    public function getStyleFromCate($cate){
        $cateData = $this->company_repository->getDataFromCate($cate);
        if(!$cateData){
            return null;
        }
        $pid = $cateData["pid"];
        if($pid==0){
            $id = $cateData["id"];
            $style = $this->company_repository->getStyleByZeroPid($id);
            return $style;
        }
        $style = $this->company_repository->getStyleByPid($pid);
        return $style;
    }
    /**zwp
     * 通过用户id，获取产品风格
     * @param integer $uid
     * @return array
     */
    public function getStyleByUid($uid){
        
        $data = $this->company_repository->getStyleByUid($uid);
        //$dataStyle = $this->match($dataStyle);
        if(!$data){
            return null;
        }
        $dataStyle = $data["styles"];
        $dataArr = json_decode($dataStyle, true);
        //var_dump($dataArr);exit();
        $styles = [];
        /*
        foreach ($dataStyle as $value){
            $str = $value;
            if(str_contains($str, '"0"')){
                continue;
            }
            $str = str_replace('["', "", $str);
            $id = str_replace('","1"]', "", $str);
            $data = $this->company_repository->getCateByStyleId(intval($id));
            $styles[] = ["id"=>intval($id) ,"name"=> $data["name"]];
        }
        */
       foreach ($dataArr as $value){
           if($value[1]=="1"){
               $id = intval($value[0]);
               $data = $this->company_repository->getCateByStyleId($id);
               $styles[] = ["id"=>intval($id) ,"name"=> $data["name"]];
           }
       }
        return $styles;
    }
    /**zwp
     * 处理类似数组的字符串，得到对应数组数据，
     * @param string $subject
     * @return array
     */
    public function match($subject){
        $pattern = "/\[\"\d+\"\,\"\d+\"\]/";
        $matches = [];
        if(preg_match_all($pattern, $subject, $matches)){
            return $matches[0];
        }
        return null;
    }
    public function saveStyle($uid,$data){
        return $this->company_repository->saveStyle($uid,$data);
    }
    public function getProTypes($uid){
        $data = $this->company_repository->getProTypes($uid);
        if(!$data){
            return null;
        }
        $data = $this->getProTypesData($data["pro_types"]);
        return $data;
    }
    public function getProTypesData($type){
        $proTypes = $type;
        $proTypes = json_decode($proTypes,true);
        //var_dump($proTypes);exit();
        $data = $this->getCateNameById($proTypes);
        return $data;
    }
    public function getCateNameById($data){
        
        $arrRes = [];
        foreach ($data as $value){
            $proTypesData = [];
            foreach ($value as $key=> $v){
                $id = intval($v);
                $dataCate = $this->company_repository->getCateByStyleId($id);
                $name = $dataCate["name"];
                $proTypesData[] = ["$key"=>$name];
            }
            $arrRes[] = $proTypesData;
        }
        return $arrRes;
    }
    
    public function saveProTypes($uid,$data){
        $this->insertCate($data);
        return $this->company_repository->saveProTypes($uid,$data);
        
    }
    public function insertCate($data){
        foreach ($data as $value){
            $flagHas = $this->company_repository->hasName($value);
            if(!$flagHas){
                $this->company_repository->insertName($value);
            }
        }
    }

    public function getDesigns($uid){
        $data = $this->company_repository->getDesigns($uid);
        if(!$data){
            return null;
        }
        $data = $this->getDesignsData($data);
        return $data;
    }
    public function getDesignsData($data){
        $data = $data["designs"];
        $data = json_decode($data,true);
        $data = $this->getCateName($data);
        return $data;
        
    }
    public function getCateName($data){
        $arrRes = [];
        foreach ($data as $value){
            //$proTypesData = [];
            //var_dump($value);exit();
            $id = intval($value["name"]);
            $dataCate = $this->company_repository->getCateByStyleId($id);
            $name = $dataCate["name"];
            $num = intval($value["num"]);
            $arrRes[] = ["name"=>$name,"num"=>$num];
            
        }
        return $arrRes;
    }
    public function saveDesigns($uid,$data){
        $dataArr = [];
        foreach ($data as $value){
            $name = $value["name"];
            $flag = $this->company_repository->hasName($name);
            if(!$flag){
                $this->company_repository->insertName($name);
                $dataArr[] = $value;
            }
        }
        return $this->company_repository->saveDesigns($uid,$dataArr);
    }
    public function getYields($uid){
        $data = $this->company_repository->getYields($uid);
        if(!$data){
            return null;
        }
        $data = $data["yields"];
        $data = json_decode($data,true);
        $dataYields["yields"] = $data;
        return $dataYields;
    }
    public function saveYields($uid,$data){
        return $this->company_repository->saveYields($uid,$data);
    }
    public function getEnvs($uid){
        $data = $this->company_repository->getEnvs($uid);
        if(!$data){
            return null;
        }
        $dataEnvs["envs"] = $data["envs"];
        return $dataEnvs;
    }
    public function saveEnvs($uid,$data){
        return $this->company_repository->saveEnvs($uid,$data);
    }
}